<script>
import requestUtil from "@/utils/RequestUtil";
import { Toast } from "vant";

export default {
  name: "careList",
  data() {
    return {
      listData: [],
      size: 8,
      current: 1,
      num: 0,
      loading: false,
    };
  },
  methods: {
    load() {
      requestUtil({
        url: `/user/attention/${this.size}/${this.current}`,
        method: "get",
        headers: {
          token: this.$store.state.userInfo.token,
        },
      }).then((res) => {
        if (res.data.code === 1) {
          for (let i = 0; i < res.data.data.records.length; i++) {
            this.listData.push(res.data.data.records[i]);
          }
          this.num = res.data.data.total;
          this.current += 1;
        }
      });
    },
    onDelete(id) {
      console.log(id);
      requestUtil({
        url: "/user/attention/" + id,
        method: "delete",
        headers: {
          token: this.$store.state.userInfo.token,
        },
      }).then((res) => {
        if (res.data.code === 1) {
          this.num -= 1;
          this.listData = this.listData.filter((item) => item.storeId !== id);
          Toast({
            message: res.data.msg,
            type: "success",
          });
        }
      });
    },
    init() {
      if (!this.$store.state.userInfo) {
        this.$router.back();
      }
      this.load();
    },
  },
  mounted() {
    this.init();
  },
};
</script>

<template>
  <van-list
    v-model="loading"
    :finished="listData.length === num"
    finished-text="没有更多了"
    @load="load"
  >
    <van-swipe-cell v-for="item in listData" :key="item.id">
      <div
        style="
          display: flex;
          align-items: center;
          padding: 0 15px;
          margin-top: 8px;
          background-color: white;
        "
      >
        <van-image
          height="85"
          style="width: 30%"
          fit="contain"
          :src="item.img"
        />
        <div style="height: 100%; padding-left: 10px">{{ item.storeName }}</div>
      </div>
      <template v-slot:right>
        <van-button
          @click="onDelete(item.storeId)"
          square
          text="删除"
          type="danger"
          style="height: 100%"
        />
      </template>
    </van-swipe-cell>
  </van-list>
</template>

<style scoped></style>
